<template>
    <div class="login_container">
        <el-row>
            <el-col :span="12" :xs="0"></el-col>
            <el-col :span="12" :xs="24">
                <el-form class="login_form" :model="loginForm" :rules="rules" ref="loginFroms">
                    <h1>Hello</h1>
                    <h2>欢迎来到硅谷甄选</h2>
                    <el-form-item prop="username">
                        <el-input :prefix-icon="User" v-model="loginForm.username"></el-input>
                    </el-form-item>
                    <el-form-item prop="password">
                        <el-input show-password :prefix-icon="Lock" type="password"
                            v-model="loginForm.password"></el-input>
                    </el-form-item>
                    <el-form-item>
                        <el-button class="login_btn" :loading="loading" type="primary" size="default"
                            @click="login">登录</el-button>
                    </el-form-item>
                </el-form>
            </el-col>
        </el-row>
    </div>
</template>

<script setup lang="ts">
import { User, Lock } from '@element-plus/icons-vue'
import { ElNotification } from 'element-plus';
import { reactive, ref } from 'vue'
import useUserStore from '@/store/modules/user';
import { useRouter,useRoute } from 'vue-router';
import { getTime } from '@/utils/time'
let $router = useRouter()
let $route = useRoute()
let useStore = useUserStore()
let loginForm = reactive({
    username: 'admin',
    password: 'atguigu123'
})

const Username = (_rule:any,value:any,callback:any)=>{
    if(value.length<5){
        callback(new Error('账号长度不能少于五位'))
    }else{
        callback()
    }
}
const Password = (_rule:any,value:any,callback:any)=>{
    if(value.length<5){
        callback(new Error('密码长度不能少于五位'))
    }else{
        callback()
    }
}

const rules = {
    username: [
        {
            validator:Username,
            trigger: 'change'
        }
    ],
    password: [
        {
            validator:Password,
            trigger: 'change'
        }
    ]
}

let loading = ref(false)
let loginFroms = ref()
const login = async() => {
    await loginFroms.value.validate()
    loading.value = true
    useStore.userLogin(loginForm).then(() => {
        let redirect:any = $route.query.redirect
        $router.push({path:redirect||'/'})
        ElNotification({
            type: "success",
            title: 'Hi，' + getTime(),
            message: "欢迎回来"
        })
        loading.value = false
    }).catch((res) => {
        loading.value = false
        ElNotification({
            type: 'error',
            message: res.message
        })

    })
}


</script>

<style scoped lang="scss">
.login_container {
    width: 100%;
    height: 100vh;
    background: url(@/assets/icons/background.jpg) no-repeat;
    background-size: cover;

    .login_form {
        position: relative;
        width: 80%;
        top: 30vh;
        background: url(@/assets/icons/login_form.png);
        background-size: cover;
        padding: 40px;

        h1 {
            color: white;
            font-size: 40px;
        }

        h2 {
            color: white;
            font-size: 20px;
            margin: 20px 0;
        }

        .login_btn {
            width: 100%;
        }
    }
}
</style>